// pages/mine/dianpuprice/dianpuprice.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
     currentindex:0,id:'',arr:[],total:''
  },
  onLoad:function(options){
      this.setData({
        id:options.id
      })
  },
  
  onShow: function () {
      this.getallcoupon()
  },

   getallcoupon(){
      wx.request({
        url: 'https://zujieba.com/Api/storeprice/details',
        method:'POST',
        data:{uid:wx.getStorageSync('uid'),id:this.data.id},
        success:(res)=>{
          console.log(res);
          let arr=res.data.data.data
          arr.forEach((item)=>{
              item.price=parseInt(item.price)
              item.number=parseInt(item.number)
          })
          this.setData({arr:arr,total:res.data.data.total})
          
        }
      })
   },
   getcurrentindex(e){
     console.log(e);
     
      this.setData({
        currentindex:e.currentTarget.dataset.index
      })
   },
   submit(){
      this.getpaymoney((res)=>{
          console.log(res);
          let code=res.data.data
          wx.request({
            url: 'https://zujieba.com/Api/storeprice/pay_store_order.html',
            data:{code:code},
            method:'POST',
            success:(res)=>{
              console.log(code);
              
              console.log(res);
              this.setData({
                appId:res.data.appId,
                nonceStr:res.data.nonceStr,
                package:res.data.package,
                paySign:res.data.paySign,
                signType:res.data.signType,
                timeStamp:res.data.timeStamp,
                
              })
              this.getpay()
             
            }
          })
        
      })
   },
  //  提交参数 
   getpaymoney(call){
       wx.request({
         url: 'https://zujieba.com/Api/storeprice/add_order',
         data:{uid:wx.getStorageSync('uid'),pid:this.data.id,fid:this.data.arr[this.data.currentindex].id},
         method:'POST',
         success:(res)=>{
           console.log(res);
           return  call(res)
           
         }
       })
   },
    //支付
    getpay(){
      wx.requestPayment(
        {
        'timeStamp': this.data.timeStamp,
        'nonceStr': this.data.nonceStr,
        'package': this.data.package,
        'signType': this.data.signType,
        'paySign': this.data.paySign,
        'success':(res)=>{
          console.log(res);
            
              wx.showToast({
                title: '支付成功',
                icon:'success',
                duration:2000
              })
              
           this.getallcoupon()
            
                
        }, })
    },
})